REM Passed parameter 1 is the path to the existing extension directory which will be deleted
REM Passed parameter 2 is the path to the Firefox executable
REM Passed parameter 3 is the path to the xpi file of the extension that will be installed
REM Passed parameter 4 is the number of seconds to wait before checking if Firefox has closed
REM Passed parameter 5 is whether to restart Firefox after installing 

SET ext_dir=%1
SET firefox=%2
SET xpi=%3
SET secs=%4
SET restart=%5
SET build=%6

REM Don't want to add extension dir if it hasn't been installed yet
IF NOT EXIST %ext_dir% GOTO done

REM Quit Firefox
TASKKILL /T /IM "firefox.exe"


REM Wait for Firefox to close before deleting the extension directory 
SET i=0
:checkreadytodelete
	TASKLIST /FI "imagename eq firefox.exe" | find "firefox.exe" > nul
	IF errorlevel 1 GOTO readytodelete
	IF %i%==30 GOTO giveup
	SET /a i=%i%+1
	
	REM Wait
	@ping 127.0.0.1 -n 2 -w 1000 > nul
	@ping 127.0.0.1 -n %secs% -w 1000> nul
	
	GOTO checkreadytodelete
	
	
REM Delete extension directory
:readytodelete
RD %ext_dir% /S /Q 


REM Install Firefox.  Should automatically close when done; no need for taskkill.
REM %firefox% -install-global-extension %xpi%

REM new!
MKDIR %ext_dir%
XCOPY %build% %ext_dir% /S

IF %restart%==0 GOTO done

REM Still need to wait till Firefox is closed before restarting Firefox.
SET i=0
:checkreadytorestart
	TASKLIST /FI "imagename eq firefox.exe" | find "firefox.exe" > nul
	IF errorlevel 1 GOTO readytorestart
	IF %i%==30 GOTO giveup
	SET /a i=%i%+1
	
	REM Wait
	@ping 127.0.0.1 -n 2 -w 1000 > nul
	@ping 127.0.0.1 -n %secs% -w 1000> nul
	
	GOTO checkreadytorestart

	
REM Start Firefox and then exit batch file
:readytorestart
REM %firefox%
GOTO done


REM Firefox wouldn't quit
:giveup
	ECHO "I gave up because you wouldn't close firefox, you bastard!"

:done